package com.cy.aircompressor.main.ui.onlinemonitor.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;

import com.cy.aircompressor.api.RepositoryImpl;
import com.cy.aircompressor.entity.sensor.Sensor;
import com.cy.aircompressor.entity.sensor.SensorLatestData;
import com.cy.aircompressor.entity.sensor.SensorStatisticsData;
import com.cy.aircompressor.entity.warning.WarningMsg;
import com.drug.lib.view.recycptr.bean.RowEntity;
import com.net.model.base.Resource;
import com.net.model.viewmodel.BaseViewModel;

import java.util.List;

/**
 * 版权：周和权 版权所有
 *
 * @author zhouhequan
 * 版本：1.0
 * 创建日期：2021/11/12 or 23:27
 * 描述：---
 */
public class OnlineMonitorViewModel extends BaseViewModel<RepositoryImpl> {
    private MediatorLiveData<Resource<RowEntity<WarningMsg>>> queryWarningByPageObservable;
    private MediatorLiveData<Resource<List<SensorLatestData>>> queryCompressorSensorDataObservable;
    private MediatorLiveData<Resource<List<SensorStatisticsData>>> querySensorStatisticsDataDataObservable;
    private MediatorLiveData<Resource<RowEntity<Sensor>>> querySensorByPageObservable;

    public OnlineMonitorViewModel(@NonNull Application application) {
        super(application);
        bindBaseModel(RepositoryImpl.get());
        queryWarningByPageObservable = new MediatorLiveData<>();
        queryCompressorSensorDataObservable = new MediatorLiveData<>();
        querySensorStatisticsDataDataObservable = new MediatorLiveData<>();
        querySensorByPageObservable = new MediatorLiveData<>();
    }


    public void queryWarningByPage(String compressorId) {
        queryWarningByPageObservable.addSource(baseModel.queryWarningByPage(compressorId), new Observer<Resource<RowEntity<WarningMsg>>>() {
            @Override
            public void onChanged(Resource<RowEntity<WarningMsg>> rowEntityResource) {
                queryWarningByPageObservable.setValue(rowEntityResource);
            }
        });
    }

    public void queryCompressorSensorData(List<String> pram) {
        queryCompressorSensorDataObservable.addSource(baseModel.queryCompressorSensorData(pram), new Observer<Resource<List<SensorLatestData>>>() {
            @Override
            public void onChanged(Resource<List<SensorLatestData>> authBeanResource) {
                queryCompressorSensorDataObservable.setValue(authBeanResource);
            }
        });
    }

    public void querySensorStatisticsData(String startTime, String endTime, String sn, Integer type) {
        querySensorStatisticsDataDataObservable.addSource(baseModel.querySensorStatisticsData(startTime, endTime, "asc", sn, type), new Observer<Resource<List<SensorStatisticsData>>>() {
            @Override
            public void onChanged(Resource<List<SensorStatisticsData>> authBeanResource) {
                querySensorStatisticsDataDataObservable.setValue(authBeanResource);
            }
        });
    }

    public void querySensorByPage(String compressorId) {
        querySensorByPageObservable.addSource(baseModel.querySensorByPage(1,100, compressorId, null, null, null, null), new Observer<Resource<RowEntity<Sensor>>>() {
            @Override
            public void onChanged(Resource<RowEntity<Sensor>> rowEntityResource) {
                querySensorByPageObservable.setValue(rowEntityResource);
            }
        });
    }

    public MediatorLiveData<Resource<RowEntity<WarningMsg>>> getQueryWarningByPageObservable() {
        return queryWarningByPageObservable;
    }

    public MediatorLiveData<Resource<List<SensorLatestData>>> getQueryCompressorSensorDataObservable() {
        return queryCompressorSensorDataObservable;
    }

    public MediatorLiveData<Resource<List<SensorStatisticsData>>> getQuerySensorStatisticsDataDataObservable() {
        return querySensorStatisticsDataDataObservable;
    }

    public MediatorLiveData<Resource<RowEntity<Sensor>>> getQuerySensorByPageObservable() {
        return querySensorByPageObservable;
    }
}
